package leetcode.method.dynamic_programming;

import java.util.Arrays;
import java.util.List;

public class Program139WordBreak {

    public Program139WordBreak() {

    }
    public boolean wordBreak(String s, List<String> wordDict) {
        int length = s.length();
        boolean[] dp = new boolean[length + 1];
        dp[0] = true;
        // 需要判断的长度
        for (int i = 1; i < length + 1; i++) {
            //  累积结果以及判断最后一个word，有任何一个满足即可
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDict.contains(s.substring(j , i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[length];
    }

    public static void main(String[] args) {
        Program139WordBreak myStack = new Program139WordBreak();
        List<String> wordDict = Arrays.asList("leet", "code");
        System.out.println(myStack.wordBreak("leetcode",wordDict));
        List<String> wordDict1 = Arrays.asList("apple", "pen");
        System.out.println(myStack.wordBreak("applepenapple",wordDict1));
        List<String> wordDict2 = Arrays.asList("cats", "dog", "sand", "and", "cat");
        System.out.println(myStack.wordBreak("catsandog",wordDict2));
    }
}
